package com.credithc.customers.manager.service.impl;

import com.credithc.baseserv.core.service.impl.BaseServiceImpl;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.mapper.PreCustomerUnifyLoginMapper;
import com.credithc.customers.base.po.PreCustomerUnifyLoginFlowPo;
import com.credithc.customers.base.ro.LoginFlowRo;
import com.credithc.customers.base.utils.ClassUtils;
import com.credithc.customers.base.vo.LoginFlowVo;
import com.credithc.customers.manager.service.LoginFlowService;
import com.credithc.customers.manager.utils.StatsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: 业务监控处理
 * @Author: fby
 * @Date: 2020/03/24 10:21
 * @Description: com.credithc.customers.manager.service.impl
 * @Version: V1.0.0
 */
@Service
@Slf4j
public class LoginFlowServiceImpl extends BaseServiceImpl<PreCustomerUnifyLoginFlowPo, LoginFlowRo, LoginFlowVo> implements LoginFlowService {

    @Autowired
    private PreCustomerUnifyLoginMapper preCustomerUnifyLoginMapper;
    @Override
    public PageInfo<LoginFlowVo> getLoginFlow(LoginFlowRo ro) {
        try {
            Integer pageNo = ro.getPageNo();
            Integer pageSize = ro.getPageSize();
            ro.setStartPage(pageSize * (pageNo - 1));
            int count = preCustomerUnifyLoginMapper.getListCount(ro);
            List<LoginFlowVo> list = preCustomerUnifyLoginMapper.getList(ro);
            List<LoginFlowVo> vos = new ArrayList<>();
            if (count > 0) {
                for (LoginFlowVo po : list) {
                    LoginFlowVo vo = new LoginFlowVo();
                    BeanUtils.copyProperties(po, vo);
                    if (po.getCreateTime() != null) {
                        vo.setCreateTime(StatsUtils.timestamp2Stirng(Timestamp.valueOf(po.getCreateTime())));
                    }
                    //处理状态
                    if (po.getStatus() != null) {
                        if ("1".equals(po.getStatus())) {
                            vo.setStatus("成功");
                        } else if ("2".equals(po.getStatus())){
                            vo.setStatus("失败");
                        } else {
                            vo.setStatus("异常");
                        }
                    }
                    vos.add(vo);
                }
            }
            return new PageInfo(vos, count, pageNo, pageSize);
        } catch (DataAccessException e) {
            log.error("查询登陆流水报错:" + ClassUtils.getClassName() + "--" + ClassUtils.getMethodName() + "--" + e.getMessage());
            throw new CustomerException(CustomerCodes.DATABASE_ERROR_QUERY);
        }
    }


}
